﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web.UI;

namespace Doo.Controls
{
    public class Parameter
    {
        public static OrderedDictionary[] ToArrayForClient(List<Parameter> list,Control parent)
        {
            OrderedDictionary[] od = new OrderedDictionary[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                od[i] = new OrderedDictionary(10);
                list[i].InitPara(od[i],parent);
            }
            return od;
        }

        /// <summary>
        /// 客户端类型别名，仅用于在set_args时使用
        /// </summary>
        protected virtual string ClientTypeAlias { get { return "p"; } }
        public string Name { get; set; }
        public string DefaultValue { get; set; }
        public object Value
        {
            get { return DefaultValue; }
        }

        public virtual void InitPara(OrderedDictionary paras,Control parent)
        {
            paras.Add("type", ClientTypeAlias);
            paras.Add("name", Name);
            if (DefaultValue!=null) paras.Add("defaultValue", DefaultValue);
        }
    }

    public class ParameterQueryString : Parameter
    {
        protected override string ClientTypeAlias
        {
            get
            {
                return "q";
            }
        }

        public string QueryName { get; set; }
        public override void InitPara(OrderedDictionary paras, Control parent)
        {
            paras.Add("queryName", QueryName);
            base.InitPara(paras,parent);
        }
    }

    public class ParameterCookie : Parameter
    {
        protected override string ClientTypeAlias
        {
            get
            {
                return "c";//cookie
            }
        }

        public string CookieName { get; set; }
        public override void InitPara(OrderedDictionary paras, Control parent)
        {
            paras.Add("cookieName", CookieName);
            base.InitPara(paras,parent);
        }
    }

    public class ParameterControl : Parameter
    {
        protected override string ClientTypeAlias
        {
            get
            {
                return "e";//control element
            }
        }

        public string ControlID { get; set; }
        public string PropertyName { get; set; }
        public override void InitPara(OrderedDictionary paras, Control parent)
        {
            var ctl = parent.FindControl(ControlID);
            if (ctl == null)
            {
                paras.Add("controlid", ControlID);
            }
            else
            {
                paras.Add("controlid", ctl.ClientID);
            }
            paras.Add("propertyName", PropertyName);
            base.InitPara(paras,parent);
        }
    }
}
